(function() {
    var vm;

    vm = new Vue({
        el: 'body',
        data: {
            list: dataFilter(RENDER.list) || [],
            order: [],
            payment: 0
        },
        computed: {
            sum: function() {
                for (var i = 0, ret = 0; i < this.order.length; i++)
                    ret += this.order[i].price * this.order[i].count;
                return ret.toFixed(2);
            },
            change: function() {
                return this.payment - this.sum >= 0 ? (this.payment - this.sum).toFixed(2) : 0;
            }
        },
        methods: {
            // 对象，遍历对象
            addOrder: function(item) {
                var i = inArray(item.pid, this.order);
                if (typeof i === 'number') {
                    this.order[i].count++;
                } else {
                    i = this.order.push(item.$data) - 1;
                    this.order[i].count = 1;
                }
            },
            removeOrder: function(item) {
                var i = inArray(item.pid, this.order);
                if (this.order[i].count <= 1) {
                    this.order.splice(i, 1);
                } else {
                    this.order[i].count--;
                }
            },
            resetOrder: function() {
                this.order = [];
            },
            submit: function() {
                var that = this,
                    url = 'order/addOrder.action',
                    json = {};
                json.list = this.order;
                json = JSON.stringify(json);
                $.post(url, {
                    'jsonParam': json,
                    'userId': RENDER.userId 
                }, function(data, status) {
                    console.log(data);
                    alert("提交成功！");
                    that.resetOrder(); // 提交后重置
                });
            },
            toggle: function(item) {
                item.$data.display = !item.$data.display;
            }
        },
        created: function() {
            $('#orderMain').show();
        }
    });


    function inArray(id, arr) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].pid === id)
                return i;
        }
        return false;
    }

    function dataFilter(list) {
        //    	if(typeof list === 'string'){
        //    		list = JSON.parseJSON(list);
        //    	}
        var i, j, p;
        list = list.list;
        for (i = 0; i < list.length; i++) {
            if (!list[i].products.length) {
                list.splice(i, 1);
                i--;
            } else {
                list[i].display = false;
                for (j = 0; j < list[i].products.length; j++) {
                    p = list[i].products[j];
                    p.count = 0;
                    p.productType && delete p.productType;
                    p.orderItems && delete p.orderItems;
                    if (!p.onSale) {
                        list[i].products.splice(j, 1);
                        j--;
                    }
                }
            }
        }
        return list;
    }
    window.vm = vm;
})();